package com.marvels.product.param.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marvels.common.constants.MarvelsConstant;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.CacheUtils;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.common.utils.MarvelsUtils;
import com.marvels.product.param.dao.McptPaymentSequenceItemMapper;
import com.marvels.product.param.dao.McptPaymentSequenceMapper;
import com.marvels.product.param.dto.BalanceTypeEnum;
import com.marvels.product.param.dto.McptPaymentSequenceDTO;
import com.marvels.product.param.dto.McptPaymentSequenceItemDTO;
import com.marvels.product.param.dto.McptPlatformFeeDTO;
import com.marvels.product.param.dto.McptProduct;
import com.marvels.product.param.service.PaymentSequenceService;
@Service
public class PaymentSequenceServiceImpl implements PaymentSequenceService {
	
	@Autowired
	private McptPaymentSequenceMapper seqMapper;
	
	@Autowired
	private McptPaymentSequenceItemMapper seqItemMapper;

	@Override
	public PageInfo<McptPaymentSequenceDTO> getPageInfoByParam(Map<String, Object> param) throws MarvelsBusinessException{
	    
		Integer currentPage = param.get("currentPage")==null?1:(Integer)param.get("currentPage");
		
		Integer pageSize = param.get("pageSize")==null?20:(Integer)param.get("pageSize");
		
		PageHelper.startPage(currentPage, pageSize);
		
		List<McptPaymentSequenceDTO>  mcptPenaltyFeeList = seqMapper.getListByParam(param);
		
		PageInfo<McptPaymentSequenceDTO> pageInfo = new PageInfo<McptPaymentSequenceDTO>(mcptPenaltyFeeList);
	    
		return pageInfo;
	}

	@Override
	public List<McptPaymentSequenceItemDTO> getInitInfo(){
		
		List<McptPaymentSequenceItemDTO> seqList = new ArrayList<McptPaymentSequenceItemDTO>();
		
		BalanceTypeEnum[] values = BalanceTypeEnum.values();
		
		for (BalanceTypeEnum balanceTypeEnum : values) {
			McptPaymentSequenceItemDTO seq = new McptPaymentSequenceItemDTO();
			seq.setFieldCode(balanceTypeEnum.getFieldCode());
			seq.setFieldName(balanceTypeEnum.getFieldName());
			seqList.add(seq);
		}
		
		return seqList;
	}

	@Override
	@Transactional
	public void insert(McptPaymentSequenceDTO dto) throws MarvelsBusinessException{
		
		String seqCode = MarvelsUtils.generateUUIDCode("HK");
		
		dto.setPaymentSequenceCode(seqCode);
		
		List<McptPaymentSequenceItemDTO> itemList = dto.getItemList();
		
		for (McptPaymentSequenceItemDTO mcptPaymentSequenceItem : itemList) {
			mcptPaymentSequenceItem.setPaymentSequenceCode(seqCode);
		}
		
		seqMapper.insert(dto);
		
		seqItemMapper.insertBatchItem(itemList);
	}

	@Override
	public void updateStatus(McptPaymentSequenceDTO dto) throws MarvelsBusinessException{
		try {
			seqMapper.updateStatus(assemblyParam(dto));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>还款参数删除异常",e);
			throw new MarvelsBusinessException(e);
		}
	}

	@Override
	public McptPaymentSequenceDTO getEntityByPrimaryKey(Long paymentSequenceId) throws MarvelsBusinessException{
		// TODO Auto-generated method stub
		McptPaymentSequenceDTO sequence = seqMapper.selectByPrimaryKey(paymentSequenceId);
		
		String sequenceCode = sequence.getPaymentSequenceCode();
		
		List<McptPaymentSequenceItemDTO> itemList =seqItemMapper.getListByCode(sequenceCode);
		
		sequence.setItemList(itemList);
		
		return sequence;
	}

	@Override
	@Transactional
	public void updateByPrimaryKey(McptPaymentSequenceDTO dto) throws MarvelsBusinessException{
		
		seqMapper.updateByPrimaryKey(dto);
		
		List<McptPaymentSequenceItemDTO> itemList = dto.getItemList();
		
		seqItemMapper.updateBatchItem(itemList);
		
	}

	@Override
	public McptPaymentSequenceDTO getPaymentSequenceByPaymentSeq(McptProduct dto) throws MarvelsBusinessException {
		return seqItemMapper.selectPaymentSequenceByPaymentSeq(dto);
	}

	public int deleteByPrimaryKey(McptPaymentSequenceDTO dto) throws MarvelsBusinessException {
		Integer count = 0;
		try {
			 count  = seqMapper.deleteByPrimaryKey(assemblyParam(dto));
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>还款参数删除异常",e);
			throw new MarvelsBusinessException(e);
		}
		return count;
	}
	/**
	 * 组装参数
	 * @param record
	 * @return
	 * @throws Exception
	 */
	private McptPaymentSequenceDTO assemblyParam(McptPaymentSequenceDTO record) throws Exception{
		String userName = MarvelsConstant.SYSTEM_USER;
		 UserDTO user = CacheUtils.getCacheUser();
		 if(user!=null){
			 userName = user.getUserName();
		 }
		 record.setUpdatedBy(userName);
		 return record;
	}


	
}
